package com.koron.css2.meterService.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.meterService.bean.MsOutInBean;
import com.koron.css2.meterService.bean.query.MsOutInQueryBean;
import com.koron.css2.meterService.mapper.MsOutInMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

/**
 * 表务中心-水表操作历史详情查询
 * @author 周利斌
 * @since 1.8
 */
public class MsInfoHistoryList implements ServerInterface{

	private static Logger log = LogManager.getLogger(MsInfoHistoryList.class);
	
	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);
		try {
			MsOutInQueryBean bean = JsonUtils.objectToPojo(req.getData(), MsOutInQueryBean.class);
			Map<String, String> dics = Tools.mapDicByCode(factory, "OPT");
			PageHelper.startPage(bean.getPage(), bean.getPageCount());
			MsOutInMapper mapper = factory.getMapper(MsOutInMapper.class);		
			List<MsOutInBean> list = mapper.selectMsOutInList(bean);
			list.stream().forEach(t -> {
				if(t.getOperateType() != null) {
					t.setOperateTypeName(dics.get(t.getOperateType() + ""));
				}
			});
			info.setData(new PageInfo<MsOutInBean>(list));
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			log.error(e.getMessage(), e);
		}
		return info;
	}

}
